home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9335 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: lrz-muenchen.de!news
  2. From: watzka@stat.uni-muenchen.de (Kurt Watzka)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: problem passing a string
  5. Date: 9 Mar 1996 15:48:37 GMT
  6. Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
  7. Distribution: world
  8. Message-ID: <4hs98l$l8i@sparcserver.lrz-muenchen.de>
  9. References: <4hrtsp$4mf@mulgave.octacon.co.uk> <4hrv48$huo@news.acns.nwu.edu>
  10. NNTP-Posting-Host: sun2.lrz-muenchen.de
  11.  
  12. muzaffar@casbah.acns.nwu.edu (Usman Muzaffar) writes:
  13.  
  14. >In article <4hrtsp$4mf@mulgave.octacon.co.uk>,
  15. >Nik Palmer <Nik.Palmer@onyx.octacon.co.uk> wrote:
  16. >>Hi,  Im using a textoutput routine in the fastgraph package of the
  17. >>form below
  18. >>    void fg_print(char *string,int n);
  19. >>The string that I want to pass is initialised as below
  20. >>
  21. >>char string[6]
  22. >>double variable= 1.2345
  23. >>sprintf(string1,"%5f",variable)
  24.  
  25. Is there any relation between "string" and "string1"?
  26.  
  27. >>
  28. >>fg_print(string1,5)
  29. >>
  30. >>problem is it's outputting garbage,
  31.  
  32. Do you observe the same behaviour when you call, e.g., puts()?
  33.  
  34. >> I know that string1 is only a
  35. >>pointer to the memory that holds the string, and that if I want to
  36. >>access the string I need string1[num].  So I think it's just
  37. >>outputting 5 bytes of memory from the memory location string1.
  38.  
  39. Since we do _not_ know what "string1" _is_ in your program, this
  40. is quite hard to answer. Since you seem to talk about an array 
  41. of char, I will assume that "string1" is declared as "char string1[6]".
  42.  
  43. In this case, string1 is an array of 6 characters, and yes, it
  44. decays to a pointer to the first element when used in an expression
  45. context. This is _not_ a problem in general.
  46.  
  47.  { 
  48.    char string[6];
  49.    double variable = 1.2345;
  50.  
  51.    sprintf(string, "%5.3f", variable);
  52.    puts(string);
  53.  }
  54.  
  55. This should work quite well, without declaring string as static.
  56.  
  57. Why did I change Nik's format string? Because the optional "width"
  58. specifier denotes the _mimimum_ width used by the output routine,
  59. and because the output will be padded to have _at least_ the
  60. desired width. This does _not_ specify a maximum width for the
  61. output produced. Therefore, "%5f" might easily use _more_ than 5
  62. characters and "overrun" the available memory. 
  63.  
  64. >>
  65. >>How do I  pass this routine the string??
  66. >>Thanks for your help.
  67. >>Nik
  68.  
  69. >I'll bet anything that "char string[6] " is declared locally
  70. >inside some function. The problem is that the pointer you pass
  71. >refers to memory that fg_print can't access. It's a valid string
  72. >pointer, alright, but not in a valid location.
  73.  
  74. >Remember, only globals & data on the heap (stuff made by malloc and calloc, for
  75. >example) is truly accessible all the time. Anything declared inside
  76. >a function (including main()) is NOT valid outside that function.
  77.  
  78. This is _not_ true. Local variables "disappear" when you leave a
  79. function, but not when you call another function from a function.
  80.  
  81. Kurt
  82. --
  83. | Kurt Watzka                             Phone : +49-89-2180-6254
  84. | watzka@stat.uni-muenchen.de
  85. | ua302aa@sunmail.lrz-muenchen.de
  86.